<template>
    <div>
         <el-dialog
            :title="giveCouponShow === 1 ? '给该批次所有学员发送卡卷' : '给该学员发放卡卷'"
            :visible.sync="giveCouponVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :show-close="false"
            width="50%">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px" class="demo-ruleForm">
                 <el-form-item label="发送类型" prop="type">
                    <el-radio-group v-model="ruleForm.type">
                        <el-radio label="0">其他</el-radio>
                        <el-radio label="1">金奖</el-radio>
                        <el-radio label="2">礼盒</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="优惠券类型选择" prop="cake_id" v-if="ruleForm.type != '2'">
                    <el-select v-model="ruleForm.cake_id" placeholder="请选择类型选择">
                        <el-option :label="option.cakeName" :value="option.cakeId" v-for="(option,oIndex) in couponTypeList" :key="oIndex"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="到期时间" prop="invalid_date" v-if="ruleForm.type === '2'">
                    <el-date-picker
                        v-model="ruleForm.invalid_date"
                        type="datetime"
                        placeholder="选择日期时间">
                    </el-date-picker>
                </el-form-item>
                <!-- <el-form-item label="是否发送微信服务通知" prop="notification">
                    <el-switch v-model="ruleForm.notification"></el-switch>
                </el-form-item> -->
                <el-form-item label="备注" prop="remark" v-if="ruleForm.type != '2'">
                    <el-input v-model="ruleForm.remark" placeholder="非必填。推荐写入。说明原因发放卡劵"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即派发</el-button>
                    <el-button @click="cancelForm(0)">取消</el-button>
                </el-form-item>
            </el-form>
         </el-dialog>
    </div>
</template>
<script>
    import {
        obtainCouponType,
        giveUserCoupon
    } from "@/config/api"
    export default {
        props: ['giveCouponShow','studentData'],
        computed: {
            giveCouponVisible () {
                // 发送卡卷弹窗 显示 0隐藏 1给该批次添加卡卷 2给该学员添加卡卷 3添加成功隐藏卡卷弹窗并刷新列表
                return this.giveCouponShow === 1 || this.giveCouponShow === 2 ? true : false;
            }
        },
        data () {
            return {
                // 优惠券类型ID
                couponTypeList: [],
                ruleForm: {
                    // 0其他 1金奖 2礼盒
                    type: '1',
                    // 时间
                    invalid_date: '',
                    // 是否发送微信服务通知
                    notification: false,
                    // 优惠券类型
                    cake_id: '',
                    // 备注
                    remark: ''
                },
                rules: {
                    type: [
                        { required: true, message: '请选择活动资源', trigger: 'change' }
                    ],
                    invalid_date: [
                        { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                    ],
                    remark: [
                        {  message: '请输入备注', trigger: 'blur' },
                        // { min: 3, max: 500, message: '长度在 3 到 500 个字符', trigger: 'blur' }
                    ],
                    cake_id: [
                        { required: true, message: '请选择优惠券', trigger: 'change' }
                    ]
                }
            }
        },
        mounted () {
            // 获取优惠券类型
            this.acquireCouponType();
        },
        methods: {
            // 获取优惠券类型
            async acquireCouponType () {
                const data = await obtainCouponType();
                if (data.status === 200) {
                    this.couponTypeList = data.data;
                }
            },
            // 重置表单
            resetForm () {
                this.$refs['ruleForm'].resetFields();
            },
            // 关闭窗口
            cancelForm (num) {
                this.resetForm();
                this.$emit('handleGiveCouponShow',num);
            },
            // 提交表单
            submitForm (formName) {
                this.$refs[formName].validate( async (valid) => {
                    if (valid) {
                        const batch_id = this.$route.query.batch_id;
                        const type = this.ruleForm.type;
                        const cake_id = this.ruleForm.cake_id;
                        const invalid_date = new Date(this.ruleForm.invalid_date).getTime();
                        const notification = this.ruleForm.notification;
                        const remark = this.ruleForm.remark;
                        const student_id = this.studentData && this.studentData.student_id ? this.studentData.student_id : '';
                        console.log(batch_id, student_id, type, cake_id, invalid_date, notification, remark);
                        try {
                            // 统一发送卡卷
                            const data = await giveUserCoupon(batch_id, student_id, type, cake_id, invalid_date, notification, remark);
                            if (data.status === 200) {
                                this.$message({
                                    message: '已成功派送卡卷',
                                    type: 'success'
                                });
                                // 重置表单
                                this.resetForm();
                                // 关闭窗口
                                this.cancelForm(3);
                            } else {
                                 this.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                            }
                        } catch(e) {
                              this.$message({
                                    message: '网络拥堵，请稍后再试。或联系开发人员',
                                    type: 'error'
                                });
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    }
</script>
